package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ControllerAdvice@ controller功能增强标签
 * 对请求方法前，后增加功能
 *
 * 请求方法前： 运用场景：统一时间参数格式
 * 请求方法后： 运用场景：统一异常处理
 */
@ControllerAdvice
public class CommonExceptionHandler {

    //异常处理标签
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object logicExp(Exception e) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false,e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false,"系统出现未知异常，请联系客服。");
    }
}
